#include "ReportGenerator.hh"
#include "Customer.hh"
#include "Account.hh"
#include "Transaction.hh"
#include "Utils.hh"  

#include <iostream>
#include <fstream>
#include <iomanip>
#include <sstream>

using namespace std;

namespace ReportGenerator {

    //  使用 Utils 中的时间格式化函数
    void printTransaction(ofstream& outFile, const shared_ptr<Transaction>& t) {
        outFile << "Transaction ID : " << t->getID() << "\n";
        outFile << "Type           : " << Utils::transactionTypeToString(t->getType()) << "\n";
        outFile << "Amount         : ¥" << fixed << setprecision(2) << t->getAmount() << "\n";
        outFile << "Time           : " << Utils::formatTime(t->getTime()) << "\n";
        outFile << "From Account   : " << (t->getFrom().empty() ? "N/A" : t->getFrom()) << "\n";
        outFile << "To Account     : " << (t->getTo().empty() ? "N/A" : t->getTo()) << "\n";
        outFile << "Note           : " << t->getNote() << "\n";
        outFile << "---------------------------------------------\n";
    }

    void generateGlobalTransactionReport(const vector<shared_ptr<Customer>>& customers) {
        string date = Utils::getCurrentDate();  //  使用工具类
        string filename = "Reports/transactions_" + date + ".txt";
        ofstream outFile(filename);

        if (!outFile) {
            cerr << " Failed to create file: " << filename << endl;
            return;
        }

        outFile << "===== Global Transaction Report (" << date << ") =====\n\n";

        for (const auto& customer : customers) {
            for (const auto& account : customer->getAccounts()) {
                for (const auto& t : account->getTransactions()) {
                    printTransaction(outFile, t);
                }
            }
        }

        outFile.close();
        cout << " Global transaction report generated: " << filename << "\n";
    }

    void generateCustomerTransactionReport(shared_ptr<Customer> customer) {
        string date = Utils::getCurrentDate();  // 使用工具类
        string filename = "Reports/transactions_" + customer->getName() + "_" + date + ".txt";
        ofstream outFile(filename);

        if (!outFile) {
            cerr << " Failed to create file: " << filename << endl;
            return;
        }

        outFile << "===== Transaction Report for Customer: " << customer->getName()
                << " (" << date << ") =====\n\n";

        for (const auto& account : customer->getAccounts()) {
            for (const auto& t : account->getTransactions()) {
                printTransaction(outFile, t);
            }
        }

        outFile.close();
        cout << " Customer transaction report generated: " << filename << "\n";
    }

    void generateAccountTransactionReport(Account* account) {
        string date = Utils::getCurrentDate();  //  使用工具类
        string filename = "Reports/transactions_" + account->getID() + "_" + date + ".txt";
        ofstream outFile(filename);

        if (!outFile) {
            cerr << " Failed to create file: " << filename << endl;
            return;
        }

        outFile << "===== Transaction Report for Account: " << account->getID()
                << " (" << date << ") =====\n\n";

        for (const auto& t : account->getTransactions()) {
            printTransaction(outFile, t);
        }

        outFile.close();
        cout << " Account transaction report generated: " << filename << "\n";
    }

}  
